package com.black.app.service.impl.find;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.find.AppFindZhangzhishiZan;
import com.black.app.domain.req.find.add.Zhangzhishizanaddreq;
import com.black.app.domain.req.find.query.AppFindZhangzhishidianzanreq;
import com.black.app.mapper.find.AppFindZhangzhishiZanMapper;
import com.black.app.service.find.AppFindZhangzhishiZanService;
import com.black.common.constant.CacheConstants;
import com.black.common.exception.ServiceException;
import com.black.common.exception.database.PrimaryException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 30223
* @description 针对表【app_find_zhangzhishi_zan(涨知识-文章点赞表)】的数据库操作Service实现
* @createDate 2024-10-25 11:59:26
*/
@Service
public class AppFindZhangzhishiZanServiceImpl extends ServiceImpl<AppFindZhangzhishiZanMapper, AppFindZhangzhishiZan>
    implements AppFindZhangzhishiZanService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<AppFindZhangzhishiZan> paginationQuery(AppFindZhangzhishidianzanreq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    @Override
    public boolean add(Zhangzhishizanaddreq req) {
        //验证点赞人id和点赞id是否重复
        if(this.baseMapper.exists(new LambdaQueryWrapper<AppFindZhangzhishiZan>().eq(AppFindZhangzhishiZan::getZanuserid,req.getZanuserid()))){
            throw new ServiceException("点赞人id重复");
        }
        AppFindZhangzhishiZan entity = new AppFindZhangzhishiZan();
        BeanUtils.copyProperties(req, entity);
        boolean save = this.save(entity);
        if (save){
            //点赞成功后，将点赞信息存入redis
            redisTemplate.opsForValue().set(CacheConstants.APP_FIND_ZHANGZHISHI_ZAN_KEY+req.getZanuserid(),entity);
        }
        return save;
    }

    @Override
    public boolean deletezanid(Long[] ids) {
        final AppFindZhangzhishiZanServiceImpl appFindZhangzhishiZanService = this;
        if (Objects.isNull(ids) || ids.length == 0) {
            throw new PrimaryException();
        }

        this.redisTemplate.execute(new SessionCallback<Void>() {

            @Override
            public Void execute(RedisOperations operations) throws DataAccessException {
                operations.multi();

                try {
                    for (Long id : ids) {
                        AppFindZhangzhishiZan entity = appFindZhangzhishiZanService.getById(id);
                        if (Objects.nonNull(entity)) {
                            //删除数据库中的值
                            appFindZhangzhishiZanService.removeById(id);
                            //移出缓存中的值
                            String redisKey = CacheConstants.APP_FIND_ZHANGZHISHI_ZAN_KEY + entity.getId();
                            operations.delete(redisKey);
                        }
                    }
                    // 提交redis事务
                    operations.exec();
                }catch (Exception e) {
                    operations.discard();
                    throw e;
                }
                return null;
            }
        });
        return true;
    }
}




